﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLULearningNetwork.ActionFilterAttributes;
using DLULearningNetworkModel;
using DLULearningNetwork.Libs;

namespace DLULearningNetwork.Controllers
{
    public class NotificationController : Controller
    {
        //
        // GET: /Notification/
        [RequireUserLogin()]
        [HttpPost]
        public string DeleteNotification(Guid notificationID, string type)
        {
            int kq;
            switch (type)
            {
                case "friend":
                    kq = FriendNotification.Delete(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
                case "course":
                    kq = CourseNotification.Delete(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
                case "other":
                    kq = OtherNotification.Delete(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
            }
            return "unsuccess";
        }

        [RequireUserLogin()]
        public ActionResult OtherNotificationBox(int skip = 0, int take = 10)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            List<OtherNotification> lstOtherNotification = OtherNotification.GetNewOtherNotification(currentUser.AccountID_u, skip, take);
            LibSession.Set(Constants.LASTTIME_GET_OTHER_NOTIFICATION, DateTime.Now.ToString());
            return PartialView(lstOtherNotification);
        }

        [RequireUserLogin()]
        public ActionResult CourseNotificationBox(int skip = 0, int take = 10)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            List<CourseNotification> lstCourseNotification = CourseNotification.GetNewCourseNotification(currentUser.AccountID_u, skip, take);
            LibSession.Set(Constants.LASTTIME_GET_COURSE_NOTIFICATION, DateTime.Now.ToString());
            return PartialView(lstCourseNotification);
        }

        [RequireUserLogin()]
        public ActionResult FriendNotificationBox(int skip = 0, int take = 10)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            List<FriendNotification> lstFriendNotification = FriendNotification.GetNewFriendNotification(currentUser.AccountID_u, skip, take);
            LibSession.Set(Constants.LASTTIME_GET_FRIEND_NOTIFICATION, DateTime.Now.ToString());
            return PartialView(lstFriendNotification);
        }

        [RequireUserLogin()]
        public ActionResult AllNotification([Bind(Prefix = "id")]string notificationType = "friend")
        {
            Account currentUser = RequireUserLogin.LoginUser;
            ViewBag.NotificationType = notificationType;
            return View();
        }

        [RequireUserLogin()]
        [HttpPost]
        public ActionResult PagingNotification(string type, int page = 1)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            int totalItems = 0;
            switch (type)
            {
                case "friend":
                    List<FriendNotification> lstFriendNotification = FriendNotification.GetAllFriendNotification(currentUser.AccountID_u, page, Libs.Constants.DEFAULT_NUMBER_OF_NOTIFICATION_PERPAGE, ref totalItems);
                    ViewBag.TotalItems = totalItems;
                    ViewBag.CurrentPage = page;
                    return PartialView("FriendNotificationPage", lstFriendNotification);
                case "other":
                    List<OtherNotification> lstOtherNotification = OtherNotification.GetAllOtherNotification(currentUser.AccountID_u, page, Libs.Constants.DEFAULT_NUMBER_OF_NOTIFICATION_PERPAGE, ref totalItems);
                    ViewBag.TotalItems = totalItems;
                    ViewBag.CurrentPage = page;
                    return PartialView("OtherNotificationPage", lstOtherNotification);
                case "course":
                    List<CourseNotification> lstCourseNotification = CourseNotification.GetAllCourseNotification(currentUser.AccountID_u, page, Libs.Constants.DEFAULT_NUMBER_OF_NOTIFICATION_PERPAGE, ref totalItems);
                    ViewBag.TotalItems = totalItems;
                    ViewBag.CurrentPage = page;
                    return PartialView("CourseNotificationPage", lstCourseNotification);
            }
            return PartialView();
        }

        [RequireUserLogin()]
        [HttpPost]
        public string HasReadNotification(Guid notificationID, string type)
        {
            int kq;
            switch (type)
            {
                case "friend":
                    kq = FriendNotification.UpdateReadState(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
                case "course":
                    kq = CourseNotification.UpdateReadState(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
                case "other":
                    kq = OtherNotification.UpdateReadState(notificationID, RequireUserLogin.LoginUser.AccountID_u);
                    if (kq == 1)
                    {
                        return "success";
                    }
                    break;
            }
            return "unsuccess";
        }

        [RequireUserLogin()]
        [HttpPost]
        public ActionResult GetLatestNotification(string type)
        {
            Account currentUser = RequireUserLogin.LoginUser;
            DateTime lasttime;
            switch (type)
            {
                case "friend":
                    lasttime = DateTime.Parse(LibSession.Get(Constants.LASTTIME_GET_FRIEND_NOTIFICATION) as string);
                    Libs.LibSession.Set(Constants.LASTTIME_GET_FRIEND_NOTIFICATION, DateTime.Now.ToString());
                    List<FriendNotification> lstFriendNotification = FriendNotification.GetLatestFriendNotification(currentUser.AccountID_u, lasttime);
                    return PartialView("FriendNotificationBox", lstFriendNotification);
                case "other":
                    lasttime = DateTime.Parse(LibSession.Get(Constants.LASTTIME_GET_OTHER_NOTIFICATION) as string);
                    Libs.LibSession.Set(Constants.LASTTIME_GET_OTHER_NOTIFICATION, DateTime.Now.ToString());
                    List<OtherNotification> lstOtherNotification = OtherNotification.GetLatestOtherNotification(currentUser.AccountID_u, lasttime);
                    return PartialView("OtherNotificationBox", lstOtherNotification);
                    
                case "course":
                    lasttime = DateTime.Parse(LibSession.Get(Constants.LASTTIME_GET_COURSE_NOTIFICATION) as string);
                    Libs.LibSession.Set(Constants.LASTTIME_GET_COURSE_NOTIFICATION, DateTime.Now.ToString());
                    List<CourseNotification> lstCourseNotification = CourseNotification.GetLatestCourseNotification(currentUser.AccountID_u, lasttime);
                    return PartialView("CourseNotificationBox", lstCourseNotification);
            }
            return null;
        }

       
    }
}
